using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using Graffiti.Core;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace Graffiti.Redirect
{
    public class DynamicListElement : FormElement
    {
        private string jqueryja = @"
                            <script>
                            function addRow()
			                {		
                                var tbody = $('#rowtable');

                                $('#rowtable').createAppend('tr', {}, [
                                    
                                    'td', {}, [
                                        'input', { id: 'regex', name: 'regex' }, []
                                    ],

                                    'td', {}, [
                                        'input', { id: 'target', name: 'target' }, []
                                    ],

                                    'td', {}, [],

                                    'td', {}, [
                                        'select', { id: 'sptarget', name: 'sptarget' }, [
                                            'option' , { }, 'Optional'
                                            'option' , { }, 'Post ID'
                                            'option' , { }, 'Post Name'
                                            ]
                                    ],

                                    'td', {}, [
                                        'input', { id: 'redirectType', name: 'redirectType' }, [
                                            'option' , { }, '301'
                                            'option' , { }, 'Normal'
                                            ]
                                    ]
";




        private string js = @"
                            <script>
                            function addRow()
			                {		
                                var tbody = $('rowtable');

                                var row = document.createElement('TR');
                                var td0 = document.createElement('TD');
                                var td1 = document.createElement('TD');
                                var td2 = document.createElement('TD');
                                var td3 = document.createElement('TD');
                                var td4 = document.createElement('TD');

	var regex = document.createElement('INPUT')
	regex.setAttribute('id', 'regex');
	regex.setAttribute('name', 'regex');
	
    var target = document.createElement('INPUT');
	target.setAttribute('id', 'target');
	target.setAttribute('name', 'target');
	

    var specialtarget = document.createElement('SELECT');
	specialtarget.setAttribute('id', 'sptarget');
	specialtarget.setAttribute('name', 'sptarget');
	
    var option3 = document.createElement('OPTION');
    option3.text = 'Optional';
    specialtarget.options.add(option3);

    var option1 = document.createElement('OPTION');
    option1.text = 'Post ID';
    specialtarget.options.add(option1);

    var option2 = document.createElement('OPTION');
    option2.text = 'Post Name';
    specialtarget.options.add(option2);




    var redirectType = document.createElement('SELECT');
	redirectType.setAttribute('id', 'redirectType');
	redirectType.setAttribute('name', 'redirectType');

    var option3 = document.createElement('OPTION');
    option3.text = 'Permanent (301)';
    redirectType.options.add(option3);

    var option4 = document.createElement('OPTION');
    option4.text = 'Normal';
    redirectType.options.add(option4);
	
	td0.appendChild(regex);
	td1.appendChild(target);
	td2.appendChild (specialtarget);
	td3.appendChild (redirectType);
		
    row.appendChild(td0);
    row.appendChild(td1);
    row.appendChild(td4);
    row.appendChild(td2);
    row.appendChild(td3);
    tbody.appendChild(row);
			}
    </script>
        ";

        public DynamicListElement(string name, string desc, string tip)
            : base(name, desc, tip)
        {
            
        }

        public string GetOptions(string s)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<option " + checkSelected(s, "Optional") + ">Optional</option>");            
            sb.AppendLine("<option " + checkSelected(s, "Post ID") + ">Post ID</option>");
            sb.AppendLine("<option " + checkSelected(s, "Post Name") + ">Post Name</option>");
            return sb.ToString();
        }

        public string GetRedirectTypeOptions(string s)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<option " + checkSelected(s, "Permanent (301)") + ">Permanent (301)</option>");
            sb.AppendLine("<option " + checkSelected(s, "Normal") + ">Normal</option>");
            return sb.ToString();
        }

        public string checkSelected(string s, string v)
        {
            if (s == v)
                return "selected";
            else
                return string.Empty;
        }

        public override void Write(StringBuilder sb, System.Collections.Specialized.NameValueCollection nvc)
        {
            List<RedirectRecord> redirectRecords = RedirectRecord.DeSerialize(nvc);

            sb.AppendLine(js);

            sb.AppendLine(
                "<div>Enter your redirections below. Expressions will be evaluated top to bottom. To remove a redirect empty out the regex column.</div>");
            sb.AppendLine(
                "<div style=\"padding-top:10px;\">Special Target is used to target things retrieved from Graffiti, for instance you can use a regex to find the Post ID then select \"Post ID\" and the redirector will use the last match in your regex to retrieve the post.</div>");
            sb.AppendLine(
                "<div style=\"padding-top:10px;\">If you want to perform redirects on directories that don't exist, you will need to map all requests to ASP.NET using a wildcard mapping.</div>");
           
            sb.AppendLine(
                @"<div style=""padding-top:10px;"">Here are some common examples <br/>
                    <ul>
                        <li>Regex: (/d*).aspx - Graffiti Target: Post ID</li>
                        <li>Regex: /(a-shorter-post) - Graffiti Target: Post Name </li>
                        <li>Regex: /Type/(\w*) - Target: ~/{1} </li>                        
                    <ul>
                
                </div>");

            sb.AppendLine("<table cellspacing=\"5\" cellpadding=\"5\" style=\"padding-top:20px;\">");
            sb.AppendLine("<tbody name=\"rowtable\" id=\"rowtable\">");
            sb.AppendLine("<tr>");
            sb.AppendLine("<th>Regex</th>");
            sb.AppendLine("<th>Target URL</th>");
            sb.AppendLine("<th>OR</th>");
            sb.AppendLine("<th>Graffiti Target</th>");
            sb.AppendLine("<th>Redirect Type</th>");
            sb.AppendLine("</tr>");

            foreach (RedirectRecord rr in redirectRecords)
            {
                sb.AppendLine("<tr>");
                sb.AppendLine("<td><input type=\"text\" name=\"regex\" id=\"regex\" value=\"");
                sb.AppendLine(rr.Regex);
                sb.AppendLine("\"/></td>");
                sb.AppendLine("<td><input type=\"text\" name=\"target\" id=\"target\" value=\"");
                sb.AppendLine(rr.Target);
                sb.AppendLine("\"/></td>");
                sb.AppendLine("<td></td>");
                sb.AppendLine("<td><select name=\"sptarget\" id=\"sptarget\" />");
                sb.AppendLine(GetOptions(rr.SpecialTarget));
                sb.AppendLine("</select>");
                sb.AppendLine("</td>");
                sb.AppendLine("<td><select name=\"redirectType\" id=\"redirectType\" />");
                sb.AppendLine(GetRedirectTypeOptions(rr.RedirectType));
                sb.AppendLine("</select>");
                sb.AppendLine("</td>");
                sb.AppendLine("</tr>");
            }

            sb.AppendLine("<tr>");
            sb.AppendLine("<td><input type=\"text\" name=\"regex\" id=\"regex\" /></td>");
            sb.AppendLine("<td><input type=\"text\" name=\"target\" id=\"target\" /></td>");
            sb.AppendLine("<td></td>");            
            sb.AppendLine("<td><select name=\"sptarget\" id=\"sptarget\" />");
            sb.AppendLine(GetOptions(string.Empty));
            sb.AppendLine("</select>");    
            sb.AppendLine("</td>");
            sb.AppendLine("<td><select name=\"redirectType\" id=\"redirectType\" />");
            sb.AppendLine(GetRedirectTypeOptions(string.Empty));
            sb.AppendLine("</select>");
            sb.AppendLine("</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");
            sb.AppendLine("<div><input type=\"button\" onclick=\"addRow();\" value=\"Add Row\" /></div>");
            sb.AppendLine(
                "<div>This is BETA if you find any issues please report them here: <a href=\"http://code.google.com/p/graffiti-redirect/\">Graffiti Redirect</a></div>");
        }
        
    }
}
